package com.ruoyi.eqmonitor.phase1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eqmonitor.phase1.domain.OptDevicePhase1MonitorData;
import com.ruoyi.eqmonitor.phase1.rabbit.FanoutReceiver;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1MonitorDataService;
import com.ruoyi.eqmonitor.phase1.service.IOptDevicePhase1Service;

/**
 * 一期设备监测数据
 * @author breeze
 * @date 2023-03-26
 */
@RestController
@RequestMapping("/device/phase1/monitor")
public class OptDevicePhase1MonitorDataController extends BaseController{
    
    @Autowired
    private IOptDevicePhase1MonitorDataService optDevicePhase1MonitorDataService;

    @GetMapping("/list")
    public TableDataInfo<?> list(OptDevicePhase1MonitorData optDevicePhase1MonitorData){
        startPage();
        List<OptDevicePhase1MonitorData> list = optDevicePhase1MonitorDataService.selectOptDevicePhase1MonitorDataList(optDevicePhase1MonitorData);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(optDevicePhase1MonitorDataService.selectOptDevicePhase1MonitorDataById(id));
    }

    @Log(title = "一期设备监测数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptDevicePhase1MonitorData optDevicePhase1MonitorData){
    	optDevicePhase1MonitorData.setCreateTime(new Date());
    	optDevicePhase1MonitorData.setCreateBy(SecurityUtils.getUsername());
        return toAjax(optDevicePhase1MonitorDataService.save(optDevicePhase1MonitorData));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody OptDevicePhase1MonitorData optDevicePhase1MonitorData){
    	optDevicePhase1MonitorData.setUpdateTime(new Date());
    	optDevicePhase1MonitorData.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optDevicePhase1MonitorDataService.updateById(optDevicePhase1MonitorData));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "一期设备监测数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(optDevicePhase1MonitorDataService.removeByIds(Arrays.asList(ids)));
    }
    
    /**
     * 导出一期设备监测数据列表
     */
    @Log(title = "一期设备监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptDevicePhase1MonitorData optDevicePhase1MonitorData){
        List<OptDevicePhase1MonitorData> list = optDevicePhase1MonitorDataService.selectOptDevicePhase1MonitorDataList(optDevicePhase1MonitorData);
        ExcelUtil<OptDevicePhase1MonitorData> util = new ExcelUtil<OptDevicePhase1MonitorData>(OptDevicePhase1MonitorData.class);
        util.exportExcel(response, list, "一期设备监测数据数据");
    }
    
    
    /**
     * 获取设备涉及的监测目标和最新值
     * 对标com.ruoyi.eqmonitor.controller.OptDeviceMonitorLastDataController.list
     */
    @GetMapping("/latestMonitorData")
    public TableDataInfo<?> latestMonitorData(String deviceCode){
    	List<Map<String,Object>> datas = optDevicePhase1MonitorDataService.selectOptPhase1DeviceMonitorLastDataList(deviceCode);
    	for (int i = 0; i < datas.size(); i++) {
    		Map<String,Object> data = datas.get(i);
    		data.put("monitorName", FanoutReceiver.Phase1MonitorMap.get(data.get("code")));
		}
    	return getDataTable(datas);
    }
    
    /**
     * 对标com.ruoyi.eqmonitor.controller.OptDeviceMonitorDataController.deviceMonitorChartAnalyse
     */
    @GetMapping("/deviceMonitorChartAnalyse")
    public AjaxResult deviceMonitorChartAnalyse(@RequestParam("deviceCode") String deviceCode,@RequestParam(value = "type", required = false, defaultValue = "normal")String type) {
        return success(optDevicePhase1MonitorDataService.deviceMonitorChartAnalyse(deviceCode,type));
    }
}
